package com.example.zhish.newsasync_demo001;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zhish on 2016/4/12.
 */
public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {
    private List<NewsBean> list;
    private LayoutInflater inflater;

    private boolean mFirstStart;

    //当前屏幕显示的第一项与最后一项
    private int mStart;
    private int mEnd;
    //所有的URL地址
    public static String[] URLS;

    private ImageLoader imageLoader;

    public NewsAdapter(Context context, List<NewsBean> list, ListView listView) {
        this.list = list;
        inflater = LayoutInflater.from(context);
        imageLoader = new ImageLoader(listView);
        URLS = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            URLS[i] = list.get(i).getImageUrl();
        }
        listView.setOnScrollListener(this);
        mFirstStart = true;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageview1);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.content1);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
        //设置Tag，防止图片加载错位
        String url = list.get(position).getImageUrl();
        viewHolder.imageView.setTag(url);
        //线程池加载图片
        //new ImageLoader().showImageByThread(viewHolder.imageView, url);
        //AsyncTask加载

        imageLoader.showImagebyAsyncTask(viewHolder.imageView, url);
        viewHolder.tvTitle.setText(list.get(position).getNewsTitle());
        viewHolder.tvContent.setText(list.get(position).getNewsContent());
        return convertView;
    }

    class ViewHolder {
        public TextView tvTitle, tvContent;
        public ImageView imageView;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //停止状态
        if (scrollState == SCROLL_STATE_IDLE) {
            //加载可见项
            imageLoader.loadImages(mStart, mEnd);

        } else {
            //停止任务
            imageLoader.cancelAllTask();
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {


        mStart = firstVisibleItem;
        mEnd = mStart + visibleItemCount;
        //初始化预加载
        if (mFirstStart && visibleItemCount>0) {
            mFirstStart = false;
            imageLoader.loadImages(mStart, mEnd);
        }
    }


}
